import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class AnagramStringHandler {
	
		private ArrayList<Character> chars = new ArrayList<Character>();
	
		AnagramStringHandler(int num) throws IOException {
			
			BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
			String str;
			boolean isWrongInput = true;
			while (isWrongInput) {
				System.out.println("Input phrase #"+num+" (only letters and spaces are allowed): ");
				str = input.readLine();
				System.out.println("You've inputed phrase: "+str);
				str = str.toLowerCase();
				chars.clear();
				isWrongInput = false;
				for (char c : str.toCharArray()) {
					if (Character.isLetter(c)||(c==' ')) { // phrase should contain letters and spaces only
						chars.add(c);
					} else {
						isWrongInput = true;
						break;
					}
				}
				if (isWrongInput) {
					System.out.println("Wrong input. Try again.");
				}
			}	
			Iterator<Character> i = chars.iterator(); 
			while (i.hasNext()) {
				Character c = i.next(); 
				if (c==' ') {
					i.remove(); // Remove spaces in list for further comparison without spaces
				}
			}
			Collections.sort(chars); // Sort list of characters without spaces
			
		}
		
		public ArrayList<Character> getCharacterlist () {
			return this.chars;
		}
}